<?php
class QLNapTien_Model_NapTien extends Zend_Db_Table
{
	protected $_name='nap_tien';
	protected $_primary='id_nt';
	
	
// Đếm số phần tử
	public function countItem($arrParam = null, $options = null)
	{
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
			 		 ->from('nap_tien',array('COUNT(id_nt) AS totalItem'));
		$result = $db->fetchOne($select);
		return $result;
	}
	
	//danh sach rut tien
	public function listItem($arrParam = null, $option = null)
	{
  		$db = Zend_Registry::get('connectDB');
  		if($option ==  null)
  		{  		
  			
  		  $sql = $db->select()
					->from($this->_name,array('id_nt','user_name','tien_hien_tai','so_tien_nap','ngay_nap',
												'tong_tien' => new Zend_Db_Expr ('tien_hien_tai + so_tien_nap')));
			
		$result = $db->fetchAll($sql);
 		}
  		
  		return $result;
  	}
public function getItem($arrParam = null, $option = null)
	{
  		$db = Zend_Registry::get('connectDB');
  		if($option ==  null){  		
  		  			$sql = $db->select()
							->from($this->_name,array('id_nt','user_name','tien_hien_tai','so_tien_nap','ngay_nap'))
							->where('id_nt = ?',$arrParam['id']);
			$result = $db->fetchRow($sql);
 		}
  		
  		return $result;
  	}

  	//luu du lieu vao database
  	public function saveItem($arrParam=null,$option=null)
  	{
  		if ($option['task']=='add')
  		{
  			$row=$this->fetchNew();
  			$row->id_nt				=$arrParam['id_nt'];
  			$row->user_name			=$arrParam['user_name'];
  			$row->tien_hien_tai	=$arrParam['tien_hien_tai'];
  			$row->so_tien_nap		=$arrParam['so_tien_nap'];
  			$row->ngay_nap	=$arrParam['ngay_nap'];
  			$row->save();	
  		}
  		if ($option['task']=='edit')
  		{
  			$where='id_nt=' .'"'.$arrParam['id'].'"';

  			$row=$this->fetchRow($where);
  			$row->user_name			=$arrParam['user_name'];
  			$row->tien_hien_tai	=$arrParam['tien_hien_tai'];
  			$row->so_tien_nap=$arrParam['so_tien_nap'];
  			$row->ngay_nap	=$arrParam['ngay_nap'];
  			$row->save();
  		}
  	}
  	public function deleteItem($arrParam=null,$option=null)
  	{
  		if ($option['task']=='delete')
  		{
  			$where='id_nt= '.' "'.$arrParam['id']. '"';
  			$this->delete($where);
  		}
  	}
}
